package com.guanjia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guanjia.entity.Bussiness;
import com.guanjia.entity.CreditInformation;
import com.guanjia.entity.CrowdFunding;
import com.guanjia.entity.User;
import com.guanjia.entity.vo.CrowdDeal;
import com.guanjia.entity.vo.CrowdUserInfVo;
import com.guanjia.mapper.BussinessMapper;
import com.guanjia.mapper.CrowdDealMapper;
import com.guanjia.mapper.CrowdFundingMapper;
import com.guanjia.mapper.UserMapper;
import com.guanjia.service.CrowdFundingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CrowdFundingServiceImpl implements CrowdFundingService {
    public static Integer CURRENT_PAGE = 1;
    public static Integer PAGE_SIZE = 5;

    @Autowired
    private CrowdFundingMapper crowdFundingMapper;

    @Autowired
    private BussinessMapper bussinessMapper;

    @Autowired
    private CrowdDealMapper crowdDealMapper;
    @Autowired
    private UserMapper userMapper;


    @Override
    public void insertForm(CrowdFunding crowdFunding) {
        crowdFundingMapper.insert(crowdFunding);
    }

    @Override
    public List<CrowdFunding> handList() {
        List<CrowdFunding> handlist =  crowdFundingMapper.handList();
        return handlist;
    }

    @Override
    public List<CrowdFunding> newList() {
        List<CrowdFunding> newList = crowdFundingMapper.newList();
        return newList;
    }

    @Override
    public CrowdFunding findCrowdById(int id) {
        return crowdFundingMapper.findCrowFuningById(id);
    }

    @Override
    public CrowdUserInfVo findCrowdUser(int userId) {
        return crowdFundingMapper.findCrowUserInf(userId);
    }
    @Override
    @Transactional
    public String insertDealAndBussiness(String money, String crowdId, Integer userId,Integer crowd_user_id) {
        System.out.println(Double.valueOf(money) instanceof Double);

            User u1=userMapper.findUserById(userId);
            User u2=userMapper.findUserById(crowd_user_id);
            CrowdDeal crowdDeal=new CrowdDeal();
            crowdDeal.setUser_id(userId);
            crowdDeal.setCrowd_id(Integer.parseInt(crowdId));
            crowdDeal.setMoney(Double.valueOf(money));
            crowdDealMapper.insert(crowdDeal);
            Bussiness bussiness=new Bussiness();//支出信息
            bussiness.setUserId(userId);
            bussiness.setBalanceout(Double.valueOf(money));
            bussiness.setType("4");
            bussiness.setBalance(u1.getBalance()-Double.valueOf(money));
            u1.setBalance(u1.getBalance()-Double.valueOf(money));
            userMapper.updateById(u1);
            bussinessMapper.insertBussiness(bussiness);
            Bussiness crowd_user=new Bussiness();//  存入的信息
            crowd_user.setUserId(crowd_user_id);
            crowd_user.setBalancein(Double.valueOf(money));
            crowd_user.setType("4");
            crowd_user.setBalance(u2.getBalance()+Double.valueOf(money));
            u2.setBalance(u2.getBalance()+Double.valueOf(money));
            userMapper.updateById(u2);
            bussinessMapper.insertBussiness(crowd_user);
            CrowdFunding crowdFunding = crowdFundingMapper.findCrowFuningById(Integer.parseInt(crowdId));
            crowdFunding.setNowmoney(crowdFunding.getNowmoney()+Double.valueOf(money));

            crowdFundingMapper.updateById(crowdFunding);

        return "ok";
    }

    @Override
    public List<CrowdFunding> findCrowdFundingListById(Integer id) {
        return crowdFundingMapper.findCrowdFundingListById(id);
    }

    @Override
    public IPage<CrowdFunding> showList(Integer page, Integer size, String sort, String pname, Integer max, Integer min) {
        if (page != null) {
            CURRENT_PAGE = page;
        }
        if (size != null) {
            PAGE_SIZE = size;
        }
        QueryWrapper<CrowdFunding> queryWrapper=new QueryWrapper<CrowdFunding>().ne("status",0);
        if(sort!=null&&sort.equals("1")){
            queryWrapper =  new QueryWrapper<CrowdFunding>().orderByDesc("rateofreturn").ne("status",0);
        }
        if(pname!=null){
            queryWrapper =  new QueryWrapper<CrowdFunding>().like("projectname",pname).ne("status",0);
        }
        if (max!=null||min!=null){
            queryWrapper =  new QueryWrapper<CrowdFunding>().between("money",min,max).ne("status",0);
        }
        IPage<CrowdFunding> list = crowdFundingMapper.selectPage(
                new Page<>(CURRENT_PAGE, PAGE_SIZE),
                queryWrapper
        );
        return list;
    }


}
